/****************************************************************************
 *  Sample client program to test the following XP functions:
 *     XPmod()
 *     XPdiv()
 *     XPmul()
 *     XPrand()
 *
 *  Compilation:  gcc126 testdiv.c xp.c
 *  Execution:    a.out
 *
 *  If anything is printed, you probably have a bug.
 *
 ****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "XP.h"
#define TRIALS 500


/**************************************************
 *  q = a / b
 *  r = a % b
 *  check that a = q * b + r
 **************************************************/
int test_div(XP a, XP b, XP q, XP r) {
  if(!XPeq(XPadd(XPmul(q, b), r), a) || (!XPless(r, b)) ) {
      printf("Error detected while running testdiv.c\n");
      printf("a               = "); XPshowDecimal(a); printf("\n");
      printf("b               = "); XPshowDecimal(b); printf("\n");
      printf("q = XPdiv(a, b) = "); XPshowDecimal(q); printf("\n");
      printf("r = XPmod(a, b) = "); XPshowDecimal(r); printf("\n");
      printf("q * b + r       = "); XPshowDecimal(XPadd(XPmul(q, b), r)); printf("\n");
      printf("\n");
      return 1;
   }

   return 0;
}


/************************************************************
 *  Sample client to help test multiply.
 *  Assumes XPeq(), XPadd(), XPmul(), and XPrand() work correctly.
 ************************************************************/
bool test_div(void) {
   bool result = true;
   int i;
   XP a, b, q, r;

   srand((unsigned int) time(NULL));

   // check division for internal consistency
   for (i = 0; i < TRIALS; i++) {
      a = XPrand(N);
      b = XPrand(N/2);
      q = XPdiv(a, b);
      r = XPmod(a, b);
      if (test_div(a, b, q, r)) {result = false; break; }
   }

   return result;
}

